//
//  ReadViewController.m
//  aMoment
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "ReadViewController.h"
#import "AllHeaderFiles.h"

@interface ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, assign)NSInteger start;
@property(nonatomic, retain)TopViewOfScrollView *scrollView;
@property(nonatomic, retain)MBProgressHUD *progressView;
@end

@implementation ReadViewController

- (void)dealloc
{
    [_collectionView release];
    [_scrollView release];
    [_progressView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"阅读";
    self.dataSourceArray = [NSMutableArray array];
    [self getData];
    
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getData];
    }];
//    [_collectionView.header beginRefreshing];
}

#pragma mark 获取数据
- (void)getData
{
    [NetworkRequest getDataWithURLString:URLSTRING_READ BodyString:BODYSTRING_READ DataBlock:^(id data) {
        [self.dataSourceArray removeAllObjects];
        NSArray *listArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            Read *read = [[Read alloc] init];
            [read setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:read];
        }
        
        //轮播图
        NSArray *carouselArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"carousel"];
        [self createScrollViewWithCarouselArray:carouselArray];
        
        [_collectionView reloadData];
        [_collectionView.header endRefreshing];
        
        //移除加载中提示视图
        [_progressView removeFromSuperview];
    }];
}

#pragma mark 添加轮播图
- (void)createScrollViewWithCarouselArray:(NSArray *)carouselArray
{
    _scrollView = [[TopViewOfScrollView alloc] initWithFrame:CGRectMake(15 * FIT_WIDTH, 15 * FIT_WIDTH, SCREEN_WIDTH - 30 * FIT_WIDTH, 155 * FIT_HEIGHT)];
    _scrollView.carouselArray = carouselArray;
    [_collectionView addSubview:_scrollView];
    [_scrollView release];
}

- (void)loadView
{
    [super loadView];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];//105 * FIT_WIDTH
    flowLayout.itemSize = CGSizeMake(105 * FIT_WIDTH, 105 * FIT_WIDTH);
    flowLayout.minimumLineSpacing = 15 * FIT_WIDTH;
    flowLayout.minimumInteritemSpacing = 14.9 * FIT_WIDTH;
    flowLayout.sectionInset = UIEdgeInsetsMake(155 * FIT_HEIGHT + 30 * FIT_WIDTH, 15 * FIT_WIDTH, 15 * FIT_WIDTH, 15 * FIT_WIDTH);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[ReadCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    //创建加载中提示视图
    self.progressView = [LoadingProgress createViewWithSuperviewFrame:self.view.frame];
    [self.view addSubview:_progressView];
    [_progressView release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.read = self.dataSourceArray[indexPath.row];
    return cell;
}

#pragma mark cell点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Read *read = self.dataSourceArray[indexPath.row];
    ReadDetailsViewController *detailsVC = [[ReadDetailsViewController alloc] init];
    [self.navigationController pushViewController:detailsVC animated:YES];
    detailsVC.type = [NSString stringWithFormat:@"%@", read.type];
    detailsVC.name = read.name;
//    [detailsVC release];//不能release
}

#pragma mark vc即将消失
- (void)viewWillDisappear:(BOOL)animated
{
    //关闭定时器
    [_scrollView.timer setFireDate:[NSDate distantFuture]];
}

#pragma mark vc即将出现
- (void)viewWillAppear:(BOOL)animated
{
    //打开定时器
    [_scrollView.timer setFireDate:[NSDate distantPast]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
